<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>运维系统字典添加</title>
    <link rel="stylesheet" type="text/css" href="/admin/css/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="/admin/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="/admin/css/bootstrap-table.min.css"/>
    <link rel="stylesheet" type="text/css" href="/admin/css/style.min.css" />
    <link rel="stylesheet" type="text/css" href="/admin/layui/css/layui.css">

    <script src="/admin/js/jquery-3.3.1.min.js"></script>
    <script src="/admin/layui/layui.js" charset="utf-8"></script>

    <link rel="stylesheet" href="/lib/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="/admin/css/ztree_select.css" type="text/css">
    <script type="text/javascript" src="/lib/zTree/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="/lib/zTree/js/jquery.ztree.excheck-3.5.js"></script>
    <script type="text/javascript" src="/lib/zTree/js/jquery.ztree.exedit-3.5.js"></script>

    <script src="/admin/js/api.js" charset="utf-8"></script>
    <script src="/admin/js/base.js" charset="utf-8"></script>
    <script src="/admin/js/common.js"></script>
    <script src="/admin/js/style.js"></script>
    <script src="/admin/js/tree.js"></script>
</head>
<body>
<div class="tab-content form-content">
    <div class="rigthContent">
        <form class="layui-form layui-form-content" lay-filter="add_form" action="">
            <div class="tab-content form-content">
                <div class="row tab-pane active" role="tabpanel" id="form-tab1">
                    <div class="col-sm-12">
                        <div class="ibox float-e-margins">
                            <div class="" >
                                <div class="layui-form-item">
                                    <div class="layui-inline width-100">
                                        <label class="layui-form-label"><samp
                                                style="color: red;">*</samp>父级栏目：</label>
                                        <div class="layui-input-block layui-input-inline-tree">
                                            <div class="layui-form-select">
                                                <div class="layui-select-title">
                                                    <input id="parentName" name="parentName" value=""
                                                           class="layui-input" type="text" placeholder="顶级栏目"
                                                           autocomplete="off">
                                                    <i class="layui-edge"></i>
                                                </div>
                                            </div>
                                            <div id="pageContent" class="treeContent selectTreeContent">
                                                <div class="manageeditdiv">
                                                    <ul id="treeDemo" class="ztree"
                                                        style="margin-top: 0px;  -moz-user-select: none;"></ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline width-100">
                                        <label class="layui-form-label"><samp
                                                style="color: red;">*</samp>字典名称：</label>
                                        <div class="layui-input-block">
                                            <input type="text" id="name" name="name" lay-verify="required"
                                                   autocomplete="on" placeholder="请输入字典名称" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline width-100">
                                        <label class="layui-form-label"><samp
                                                style="color: red;">*</samp>Code码：</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="code" id="code" lay-verify="required"
                                                   placeholder="请输Code码" autocomplete="off" class="layui-input ">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline width-100">
                                        <label class="layui-form-label"><samp
                                                style="color: red;">*</samp>排序值：</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="orderValue" id="orderValue"
                                                   lay-verify="required|number" placeholder="请输排序值"
                                                   autocomplete="off" class="layui-input ">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                        <div class="layui-inline width-100">
                                            <label class="layui-form-label layui-textarea-lable">备注：</label>
                                            <div class="layui-input-block">
                                                <textarea id="remark" name="remark" placeholder="请输入备注"
                                                          class="form-control form-control-introduce-max"></textarea>
                                            </div>
                                        </div>
                                    </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-submit">
                <input type="hidden" name="parentId" id="parentId" value="">
                <button id="save" type="button" class="layui-btn" lay-submit="" lay-filter="save" id="save">提交</button>
                <a class="layui-btn handle_back" onclick="toClose()">取消</a>
            </div>
        </form>
    </div>
</div>
<script>
    var id = getParam("id");
    var parentId = getParam("parentId");
    var update = getParam("update");
    var treeNodeJson = JSON.parse(getParam("treeNodeJson", "{}"));

    treeOption = {
        treeList: Api.admin.base.dataDictionary.getDataDictionaryList,
        selectTree: 1,
        justTree: 1
    };

    layuiReady(function () {
        _loading();
        if (update) {
            //编辑的时候，带上parentId
            initParent(parentId);
            dataView(id);
        } else {
            //添加的时候，当前id作为父级id
            initParent(id);
        }
        _loadingClose();

        //监听提交
        layform.on('submit(save)', function (data) {
            data = data.field;
            if (update) {
                //编辑的时候，设置id
                data.dataDictionaryId = id;
            }
            layer.confirm("确定提交吗？", {
                btn: ['确定', '取消']
            }, function (index) {
                This.jqueryAjax(Api.admin.base.dataDictionary.addDataDictionary, data, "json", function (data) {
                    if (data.status == 1) {
                        var dataObj = data.object;
                        var name = dataObj.name;
                        var id = dataObj.id;
                        var pId = dataObj.parentId;
                        var isTop = false;
                        if (!pId || pId == '0') {
                            isTop = true;
                        }
                        if (update) {
                            treeNodeJson.name = name;
                            getTopParent().updateNode(treeNodeJson);
                        } else {
                            getTopParent().addNode(name, id, pId, isTop);
                        }
                        toClose();
                        // 提示消息
                        layerMessage(data.message, 1);
                    } else {
                        reSubmit();
                        layerMessageOk(data.message);
                    }
                });
            });
            return;
        });
        _loadingClose();
    });

    function initParent(id) {
        This.jqueryAjax(Api.admin.base.dataDictionary.getDataDictionaryById, {id: id}, "json", function (data) {
            if (data.status == 1) {
                $("#parentId").val(id);
                $("#parentName").val(data.object.name);
            }
        });
    }

    // 编辑信息回显
    function dataView() {
        This.jqueryAjax(Api.admin.base.dataDictionary.getDataDictionaryById, {id: id}, "json", function (data) {
            if (data.status == 1) {
                valData(data.object);
            } else {
                layerMessage(data.message, 2);
                reSubmit();
            }
        });
    }

</script>
</body>
</html>